package com.schedule.task.util.timefree;

import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 线程安全时间
 */
public class FreeTime {

    public static void main(String[] args) {
        System.out.println("当前时间:" + getNowDate());
        System.out.println("格式化当前时间:" + getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取当前时间Date
     *
     * @return Date
     */
    public static Date getNowDate() {
        return Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static String getDateFormat(Date date, String s) {
        Assert.notNull(date, "时间为空");
        Assert.notNull(s, "时间格式化字符串为空");
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern(s));
    }


}
